package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.OperationContent;
import com.novax.ex.activity.open.api.inner.OperationContentApi;

import com.novax.ex.activity.open.model.request.OperationContentRequest;
import com.novax.ex.activity.open.model.response.OperationContentResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.OperationContentService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageUtil;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;

/**
 * @Description 活动落地页文案
 * @Author ChenXi
 * @Date 2023/04/18/10:08
 */

@RestController
public class OperationContentController extends BaseActivityController implements OperationContentApi {

    @Resource
    private OperationContentService operationContentService;

    @Override
    public ReturnResult<PageUtil<OperationContentResponse>> page(String language, Long operationId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("operationId", operationId);
        PageUtil<OperationContentResponse> pageInfo = new PageUtil<>();
        pageInfo.setCondition(map);
        operationContentService.findPage(pageInfo, OperationContentResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), pageInfo);
    }

    @Override
    public ReturnResult<OperationContentResponse> query(String language, Long id) {
        OperationContent operationContent = operationContentService.find(id);
        if(Objects.nonNull(operationContent)){
            OperationContentResponse operationContentResponse = CopyUtils.copyObject(operationContent, OperationContentResponse.class);
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS), operationContentResponse);
        }
        return ReturnResult.success(getInternational(language, InternationalCode.NOT_FOUND));
    }

    @Override
    public ReturnResult<?> add(String languages, OperationContentRequest operationContentRequest,
                               BindingResult bindingResult) {
        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LACK_VALUE));
        }
        // 判断此活动id和语言是否已配置文案
        Long count = operationContentService.queryOperationIdAndLanguage(operationContentRequest.getOperationId(),
                                                                         operationContentRequest.getLanguage());
        if(count >= 1){
            return ReturnResult.fail(getInternational(languages, InternationalCode.DUPLICATED_DATA));
        }

        OperationContent operationContent = CopyUtils.copyObject(operationContentRequest, OperationContent.class);

        boolean res = operationContentService.addSelective(operationContent);
        return res ? ReturnResult.success(getInternational(languages, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(languages, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> modify(String languages, Long id, OperationContentRequest operationContentRequest,
                                  BindingResult bindingResult) {
        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LACK_VALUE));
        }

        OperationContent operationContent = CopyUtils.copyObject(operationContentRequest, OperationContent.class);
        OperationContent currentIdContent = operationContentService.find(id);
        // 语言不可更改
        if(!currentIdContent.getLanguage().equals(operationContentRequest.getLanguage())){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LANGUAGE_CHANG));
        }

        operationContent.setId(id);
        boolean res = operationContentService.modifySelective(operationContent);
        return res ? ReturnResult.success(getInternational(languages, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(languages, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> remove(String language, Long id) {
        if(Objects.nonNull(id)){
            boolean res = operationContentService.remove(id);
            return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
                : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
        } else {
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }

    }

}
